package com.buybuyyi.stylistcrm.resource.goods.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.buybuyyi.stylistcrm.object.util.BeanTranslater;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsDao;
import com.buybuyyi.stylistcrm.resource.goods.dao.GoodsYardageDao;
import com.buybuyyi.stylistcrm.resource.goods.po.Goods;
import com.buybuyyi.stylistcrm.resource.goods.po.GoodsYardage;

import stylistcrm.core.services.goods.dto.GoodsResultDTO;
import stylistcrm.core.services.goods.dto.GoodsYardageDTO;
import stylistcrm.core.services.goods.dto.GoodsYardageDelDTO;
import stylistcrm.core.services.goods.service.IGoodsForeignService;

@Service
public class GoodsForeignService implements IGoodsForeignService{

	@Autowired
	GoodsDao goodsDao;

	@Autowired
	GoodsYardageDao goodsYardageDao;
	/**
	 * 扣减库存
	 */
	@Transactional
	@Override
	public GoodsResultDTO DeductionsGoodsYardage(List<GoodsYardageDelDTO> goodsYardageDelDtos) {
		//商品ID是否存在
		GoodsResultDTO resultDto=new GoodsResultDTO();
		resultDto.setCode("0");
		if(null == goodsYardageDelDtos || goodsYardageDelDtos.isEmpty()){
			resultDto.setMessage("参数有误");
			return resultDto;
		}
		try{
			//商品是否存在
			for(GoodsYardageDelDTO delDto : goodsYardageDelDtos){
				//查看商品
				Goods goods=goodsDao.selectByPrimaryKey(delDto.getGoodsId());
				if(null == goods){
					 throw new RuntimeException("商品不存在");
				}
				//同步锁
				synchronized (this) {
					List<GoodsYardageDTO>  goosYardageDtos=delDto.getYardageDtos();
					if(null != goosYardageDtos){
						for(GoodsYardageDTO goodYardageDto : goosYardageDtos){
						  GoodsYardage goodsYardage=new GoodsYardage();
						  goodsYardage.setColor(goodYardageDto.getColor());
						  goodsYardage.setSize(goodYardageDto.getSize());
						  goodsYardage.setGoodsId(goodYardageDto.getGoodsId());
						  //查看商品库存
						  List<GoodsYardage> goodYardages=goodsYardageDao.getGoodsYardageByGoodId(goodsYardage);
						  if(null == goodYardages || goodYardages.isEmpty()){
							  throw new RuntimeException("商品库存不存在");
						  }
						  goodsYardage =goodYardages.get(0);
						  //判断库存
						  if(goodYardageDto.getStockQuantity() >= goodsYardage.getStockQuantity()){
							  throw new RuntimeException("库存不足");
						  }
						  goodsYardage.setStockQuantity(goodsYardage.getStockQuantity()-goodYardageDto.getStockQuantity());
						  goodsYardageDao.updateByPrimaryKey(goodsYardage);
						}
					}
				}
			}
			resultDto.setMessage("扣减成功");
			resultDto.setCode("1");
       }catch(Exception e){
    	   resultDto.setMessage(e.getMessage());
		   resultDto.setCode("0");
		}
		return resultDto;
		
	}

	@Override
	public GoodsResultDTO newDeductionsGoodsYardage(List<GoodsYardageDelDTO> goodsYardageDelDtos) {
		return null;
	}

	//查询商品库存
	@Override
	public List<GoodsYardageDTO> getGoodsYardageByFGoodId(Long goodsId) {
		List<GoodsYardageDTO> goodsYardags=new ArrayList<GoodsYardageDTO>();
		//查看商品
		Goods goods=goodsDao.selectByPrimaryKey(goodsId);
		if(null == goods){
			return goodsYardags;
		}
	    GoodsYardage goodsYardage=new GoodsYardage();
	    goodsYardage.setGoodsId(goodsId);
	  //查看商品库存
	    List<GoodsYardage> goodYardages=goodsYardageDao.getGoodsYardageByGoodId(goodsYardage);
	    BeanTranslater.copyProperties(goodYardages, goodsYardags);
	    
		return goodsYardags;
	}
}
